package reduce.fz.com.reduce.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbRecycleScroollListener;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.abstracts.AbResponseListener3;
import reduce.fz.com.reduce.activity.FindCardDetailActivity;
import reduce.fz.com.reduce.adapter.FindTeamAdapter;
import reduce.fz.com.reduce.bean.Team;
import reduce.fz.com.reduce.bean.TeamCategory;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.fragment.base.BaseFragment;
import reduce.fz.com.reduce.init.RecycleOper;
import reduce.fz.com.reduce.service.FindService;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.UserUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyTeamsFragment extends BaseFragment {
    private SwipeRefreshLayout team_swipelayout;
    private RecyclerView findteam_list;
    private Activity mActivity;
    private TeamCategory topic;
    private Long uid;
    private String categoryName;
    private List<Team> teamItems;
    private Pagination pagination = new Pagination();
    private FindTeamAdapter findTeamAdapter;
    private FindService findService = ServiceBuilder.builderFindService(null);


    public MyTeamsFragment() {
        this.uid = getArguments().getLong("uid");
        this.categoryName = getArguments().getString("categoryName");
    }

    public static FindTeamFragment create(Long uid, String categoryName) {
        Bundle bundle = new Bundle();
        bundle.putLong("uid", uid);
        bundle.putString("categoryName", categoryName);
        FindTeamFragment fragment = new FindTeamFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_find_team, container, false);
    }

    public void initWidget() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.mActivity);
        team_swipelayout = (SwipeRefreshLayout) mActivity.findViewById(R.id.team_swipelayout);
        findteam_list = (RecyclerView) mActivity.findViewById(R.id.find_listview);
        RecycleOper recycleOper = new RecycleOper(mActivity, findteam_list);
        recycleOper.setDefault();
        findTeamAdapter = new FindTeamAdapter(mActivity.getBaseContext(), teamItems, new FindTeamAdapter.TeamClickListener() {

            @Override
            public void selectTeamDetail(Long uid, Long teamid) {
                joinTeam(uid,teamid);

            }
        });
        findteam_list.setAdapter(findTeamAdapter);
        findteam_list.setOnScrollListener(new AbRecycleScroollListener(linearLayoutManager) {
            @Override
            public void onLoadMore() {
                HashMap<String, Object> params = new HashMap<String, Object>();
                findService.loadFindTeam(params, pagination, new AbResponseListener3<List<Team>>() {


                    @Override
                    public void responseInfo(Result<List<Team>> data) {
                    }
                });
            }
        });
        team_swipelayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pagination.reset();
                HashMap<String, Object> params = new HashMap<String, Object>();
                findService.loadFindTeam(params, pagination, new AbResponseListener3<List<Team>>() {
                    @Override
                    public void responseInfo(Result<List<Team>> data) {
                    }
                });
            }
        });

    }
    public void joinTeam(Long uid,Long teamid)
    {
        findService.addTeam(UserUtil.getUser().getUid(), teamid, new AbResponseListener<Result>(new TypeReference<Result<Result>>(){}) {
            @Override
            public void responseInfo(Result data) {

            }
        });
    }
    public void forward(Long uid,Long teamid)
    {
        Intent intent=new Intent(MyTeamsFragment.this.getActivity(), FindCardDetailActivity.class);
        startActivity(intent);

    }

}
